package com.da.javatest.leetCode;

/**
 * @author chenlida
 * @date 2019/1/5 14:41
 * @description
 */
public class Code876 {
    public ListNode middleNode(ListNode head) {
        int max = getMax(head, 1);
        int middle = max / 2 + max % 2;
        return getListNode(head, middle);
    }

    private int getMax(ListNode node, int num) {
        if (node != null) {
            return getMax(node.next, num += 1);
        } else {
            return num;
        }
    }

    private ListNode getListNode(ListNode head, int start) {
        for (int i = 1; i < start; i++) {
            head = head.next;
        }
        return head;
    }
}
